package com.le.tester.book.concurrentchallenge.synchronizedUtil14;

/**
 * createTime：2025-05-21 10:25
 * description：使用wait()和notifyAll()来实现可重新关闭的阀门
 */
public class ThreadGate {

    private boolean isOpen;

    //    哪一代
    private int generation;

    public synchronized void close() {
        isOpen = false;
    }

    public synchronized void open() {
        ++generation;
        isOpen = true;
        notifyAll();
    }

    //    一直阻塞
    public synchronized void await() throws InterruptedException {
        int arrival = this.generation;
        while (!isOpen && arrival == generation) {
            wait();
        }

    }
}
